package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 反射机制调用方法
 */
public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.doSome();

//        //实例化
//        Class cls = Class.forName("reflect.Person");
//        Object obj = cls.newInstance();
//
//        //调用方法
//        Method method = cls.getMethod("doSome");
//        method.invoke(obj); //obj.doSome();

        Scanner scan = new Scanner(System.in);
        System.out.println("请输入类名：");
        String className = scan.next();
        System.out.println("请输入方法名：");
        String methodName = scan.next();

        Class cls = Class.forName(className);
        Object obj = cls.newInstance();
        Method method = cls.getMethod(methodName);
        method.invoke(obj);
    }
}
